
const _interopRequireDefault = require('@babel/runtime/helpers/interopRequireDefault');

exports.__esModule = true;
exports.default = void 0;

const _extends2 = _interopRequireDefault(require('@babel/runtime/helpers/extends'));

const _utils = require('../utils');

const _system = require('../utils/validate/system');

const _cell = _interopRequireDefault(require('../cell'));

const _field = _interopRequireDefault(require('../field'));

// Utils
// Components
const _createNamespace = (0, _utils.createNamespace)('address-edit-detail');
const createComponent = _createNamespace[0];
const bem = _createNamespace[1];
const t = _createNamespace[2];

const android = (0, _system.isAndroid)();

const _default = createComponent({
  props: {
    value: String,
    errorMessage: String,
    focused: Boolean,
    detailRows: [Number, String],
    searchResult: Array,
    detailMaxlength: [Number, String],
    showSearchResult: Boolean,
  },
  computed: {
    shouldShowSearchResult: function shouldShowSearchResult() {
      return this.focused && this.searchResult && this.showSearchResult;
    },
  },
  methods: {
    onSelect: function onSelect(express) {
      this.$emit('select-search', express);
      this.$emit('input', (`${express.address || ''} ${express.name || ''}`).trim());
    },
    onFinish: function onFinish() {
      this.$refs.field.blur();
    },
    genFinish: function genFinish() {
      const h = this.$createElement;
      const show = this.value && this.focused && android;

      if (show) {
        return h('div', {
          class: bem('finish'),
          on: {
            click: this.onFinish,
          },
        }, [t('complete')]);
      }
    },
    genSearchResult: function genSearchResult() {
      const _this = this;

      const h = this.$createElement;
      const { value } = this;
      const { shouldShowSearchResult } = this;
      const { searchResult } = this;

      if (shouldShowSearchResult) {
        return searchResult.map((express) => h(_cell.default, {
          key: express.name + express.address,
          attrs: {
            clickable: true,
            border: false,
            icon: 'location-o',
            label: express.address,
          },
          class: bem('search-item'),
          on: {
            click: function click() {
              _this.onSelect(express);
            },
          },
          scopedSlots: {
            title: function title() {
              if (express.name) {
                const text = express.name.replace(value, `<span class=${bem('keyword')}>${value}</span>`);
                return h('div', {
                  domProps: {
                    innerHTML: text,
                  },
                });
              }
            },
          },
        }));
      }
    },
  },
  render: function render() {
    const h = arguments[0];
    return h(_cell.default, {
      class: bem(),
    }, [h(_field.default, {
      attrs: {
        autosize: true,
        rows: this.detailRows,
        clearable: !android,
        type: 'textarea',
        value: this.value,
        errorMessage: this.errorMessage,
        border: !this.shouldShowSearchResult,
        label: t('label'),
        maxlength: this.detailMaxlength,
        placeholder: t('placeholder'),
      },
      ref: 'field',
      scopedSlots: {
        icon: this.genFinish,
      },
      on: (0, _extends2.default)({}, this.$listeners),
    }), this.genSearchResult()]);
  },
});

exports.default = _default;
